using System;
using System.Collections.Generic;
using System.Windows.Forms;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.UI.Base;
using Habanero.UI.Win;
using DockStyle=System.Windows.Forms.DockStyle;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// This control contains the a <see cref="CollapsiblePanelGroupControlWin"/> which is configured to 
    /// Contain panels for editing the <see cref="DMClass"/>s attributes, Properties, relationships and Unique Constraints.
    /// </summary>
    public class DMClassDetailCollapsiblePanelGroupControl : PanelWin, IBusinessObjectControl
    {
        private ICollapsiblePanelGroupControl _collapsiblePanelGroupControl;
        private DMClass _dmClass;
        private DMPropertyAndRuleEditorControl _dmPropertyAndRuleEditorControl;
        private DMRelationshipsControl _dmRelationshipsControl;
        private DMUniqueConstraintAndPropertyEditorControl _uniqueConstraintPropsControl;
        private DMClassEditorControl _dmClassEditorControl;
        private DMSolution _solution;

        public DMClassEditorControl DMClassEditorControl
        {
            get { return _dmClassEditorControl; }
        }

        public DMClassDetailCollapsiblePanelGroupControl()
        {
            CreateCollapsiblePanels();
            PanelWin scrollablePanel = (PanelWin)ControlFactory.CreatePanel();
            new ColumnLayoutManager(scrollablePanel, ControlFactory);
            scrollablePanel.Controls.Add((Control) _collapsiblePanelGroupControl);
            scrollablePanel.Dock = (DockStyle)Habanero.UI.Base.DockStyle.Fill;
            scrollablePanel.AutoScroll = true;
            BorderLayoutManager layoutManager = ControlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(scrollablePanel, BorderLayoutManager.Position.Centre);
        }

        private void CreateCollapsiblePanels()
        {
            _collapsiblePanelGroupControl = new CollapsiblePanelGroupControlWin();
            _dmClassEditorControl = new DMClassEditorControl(this.ControlFactory)
                        {
                            UIViewsEditor = new PopupXmlUIViewsEditor(this.ControlFactory)
                        };
            ICollapsiblePanel collapsiblePanel = _collapsiblePanelGroupControl.AddControl(_dmClassEditorControl, "Entity Details", _dmClassEditorControl.Height);
            collapsiblePanel.Pinned = true;
            _dmPropertyAndRuleEditorControl = new DMPropertyAndRuleEditorControl(this.ControlFactory);
            collapsiblePanel = _collapsiblePanelGroupControl.AddControl(_dmPropertyAndRuleEditorControl, "Properties", _dmPropertyAndRuleEditorControl.Height);
            collapsiblePanel.Pinned = true;
            _dmRelationshipsControl = new DMRelationshipsControl(this.ControlFactory);
            _collapsiblePanelGroupControl.AddControl(_dmRelationshipsControl, "Relationships", _dmRelationshipsControl.Height);
            _uniqueConstraintPropsControl = new DMUniqueConstraintAndPropertyEditorControl(this.ControlFactory);
            _collapsiblePanelGroupControl.AddControl(_uniqueConstraintPropsControl, "Unique Constraints", _uniqueConstraintPropsControl.Height);
            _collapsiblePanelGroupControl.Height = _collapsiblePanelGroupControl.TotalExpandedHeight;
        }

        public IControlFactory ControlFactory
        {
            get { return GlobalUIRegistry.ControlFactory; }
        }

        public List<ICollapsiblePanel> PanelList
        {
            get { return _collapsiblePanelGroupControl.PanelsList; }
        }

        public DMClass DMClass
        {
            get { return _dmClass; }
            set
            {
                try
                {
                    if (_dmClass == value) return;
                    _dmClass = value;
                    
                    this.DMClassEditorControl.BusinessObject = _dmClass;
                    this.DMPropertyAndRuleEditorControl.Class = _dmClass;
                    this.DMRelationshipsControl.Class = _dmClass;
                    this.UniqueConstraintPropertyEditorControl.Class = _dmClass;
                }
                catch (Exception ex)
                {
                    GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                }
            }
        }

        public DMPropertyAndRuleEditorControl DMPropertyAndRuleEditorControl
        {
            get { return _dmPropertyAndRuleEditorControl; }
        }

        public DMRelationshipsControl DMRelationshipsControl
        {
            get { return _dmRelationshipsControl; }
        }

        public DMUniqueConstraintAndPropertyEditorControl UniqueConstraintPropertyEditorControl
        {
            get { return _uniqueConstraintPropsControl; }
        }

        public IBusinessObject BusinessObject
        {
            get { return this.DMClass; }
            set { this.DMClass = (DMClass) value; }
        }

        /// <summary>
        /// gets and sets the <see cref="DMSolution"/> used in this control
        /// </summary>
        public DMSolution Solution
        {
            get { return _solution; }
            set 
            { 
                _solution = value;
                this.DMClass = null;
                DMRelationshipSelectorControl relationshipSelectorControl = (DMRelationshipSelectorControl) this.DMRelationshipsControl.RelationshipSelector;
                relationshipSelectorControl.Solution = _solution;
            }
        }
    }
}
